What is is-weakmap?
The is-weakmap npm package is a utility that allows developers to check if a given value is a WeakMap. WeakMaps are collections of key/value pairs where the keys are weakly referenced, meaning they can be garbage collected if there is no other reference to the object. This package provides a straightforward way to determine if an object is an instance of a WeakMap, which can be useful in various programming scenarios where type checking is necessary.
What are is-weakmap's main functionalities?
Checking if a value is a WeakMap
This feature allows developers to check if a given value is an instance of WeakMap. The function returns true if the value is a WeakMap, and false otherwise. This can be particularly useful when working with data structures and ensuring that certain operations or functions are only applied to WeakMaps.
const isWeakMap = require('is-weakmap');
const wm = new WeakMap();
console.log(isWeakMap(wm)); // true
const obj = {};
console.log(isWeakMap(obj)); // false
Other packages similar to is-weakmap
is-map
Similar to is-weakmap, is-map checks if a given value is a Map. While is-weakmap focuses on WeakMaps, is-map targets Maps, which are collections of key/value pairs but with keys that are not weakly referenced. Both packages serve similar purposes in type checking but for different types of collections.
lodash.isweakmap
Part of the Lodash library, lodash.isweakmap provides a similar functionality to is-weakmap by checking if a value is a WeakMap. Lodash is a broader utility library that offers a wide range of functions for different types of checks and operations, making lodash.isweakmap a part of a larger ecosystem. Compared to is-weakmap, which is focused solely on WeakMaps, lodash.isweakmap benefits from being part of the well-established Lodash library.
is-weakmap
Is this value a JS WeakMap? This module works cross-realm/iframe, and despite ES6 @@toStringTag.
Example
var isWeakMap = require('is-weakmap');
assert(!isWeakMap(function () {}));
assert(!isWeakMap(null));
assert(!isWeakMap(function* () { yield 42; return Infinity; });
assert(!isWeakMap(Symbol('foo')));
assert(!isWeakMap(1n));
assert(!isWeakMap(Object(1n)));
assert(!isWeakMap(new Set()));
assert(!isWeakMap(new WeakSet()));
assert(!isWeakMap(new Map()));
assert(isWeakMap(new WeakMap()));
class MyWeakMap extends WeakMap {}
assert(isWeakMap(new MyWeakMap()));
Tests
Simply clone the repo, npm install
, and run npm test